<?php

/**
 * This is the model class for table "purchase_orders".
 *
 * The followings are the available columns in table 'purchase_orders':
 * @property integer $id
 * @property integer $organization_id
 * @property integer $vendor_id
 * @property integer $location_id
 * @property integer $comments
 * @property integer $status
 * @property string $date_created
 */
class PurchaseOrder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return PurchaseOrder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'purchase_orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('organization_id, vendor_id, location_id, po_number', 'required'),
			array('organization_id, vendor_id, location_id, comments, status', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, organization_id, vendor_id, location_id, comments, status, date_created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'vendor' => array(self::BELONGS_TO, 'Vendor', 'vendor_id'),
			'status' => array(self::BELONGS_TO, 'Status', 'status_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'organization_id' => 'Organization',
			'vendor_id' => 'Vendor',
			'location_id' => 'Location',
			'comments' => 'Comments',
			'status' => 'Status',
			'date_created' => 'Date Created',
			'po_number' => 'PO Number'
		);
	}
	
	public function markAsReceived()
	{
		$this->status = 5;
		$this->save();
	}

	public function getPoTotal()
	{
		$line_items = PurchaseOrderLineItems::model()->findAllByPk($this->id);
		
		$total = 0;
		
		foreach ($line_items as $line_item)
		{
			$total += ($line_item->item->getCost() * $line_item->quantity);
		}
		
		return $total;
	}


	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('organization_id',$this->organization_id);
		$criteria->compare('vendor_id',$this->vendor_id);
		$criteria->compare('location_id',$this->location_id);
		$criteria->compare('comments',$this->comments);
		$criteria->compare('status',$this->status);
		$criteria->compare('date_created',$this->date_created,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function behaviors()
	{ 
		return array( 'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', ); 
	}
}